package com.cdks.wuxiu.api.service;


import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.model.dto.CommentReplyInfoDTO;
import com.cdks.wuxiu.api.model.dto.CommentTopicInfoDTO;
import com.cdks.wuxiu.api.service.mysql.CommentReplyInfoService;
import com.cdks.wuxiu.api.service.mysql.CommentTopicInfoService;
import com.cdks.wuxiu.entity.mysql.CommentReplyInfo;
import com.cdks.wuxiu.entity.mysql.CommentTopicInfo;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;


/**
 * 评论服务实现类
 */
@Slf4j
@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentTopicInfoService commentTopicInfoService;
    @Resource
    private CommentReplyInfoService commentReplyInfoService;


    @Override
    public int saveTopic(CommentTopicInfo topicInfo) {
        boolean ret  = commentTopicInfoService.save(topicInfo);
        if (ret)
        {
            return 0;
        }
        
        return -1;
    }

    @Override
    public int addReplyCount(long topicId) {
        int ret  = commentTopicInfoService.addReplyCount(topicId);
        if (ret < 0)
        {
            return -1;
        }

        return 0;
    }

    @Override
    public List<CommentTopicInfoDTO> getTopicList(Long userId, Long destId, Integer destType, Integer type, Integer page) {
        return commentTopicInfoService.getList(userId, destId, destType, type, page);
    }

    @Override
    public CommentTopicInfo getTopicById(long topicId) {
        return commentTopicInfoService.getById(topicId);
    }

    @Override
    public CommentTopicInfo getReplyById(long replyId) {
        return commentTopicInfoService.getById(replyId);
    }

    @Override
    public int setTopicById(CommentTopicInfo topicInfo) {
        boolean ret  = commentTopicInfoService.updateById(topicInfo);
        if (ret)
        {
            return 0;
        }

        return -1;
    }

    @Override
    public int setReplyById(CommentTopicInfo replyInfo) {
        boolean ret  = commentTopicInfoService.updateById(replyInfo);
        if (ret)
        {
            return 0;
        }

        return -1;
    }

    @Override
    public List<CommentReplyInfoDTO> getReplyList(Long userId, Long topicId, Integer type, Integer page) {
        return commentReplyInfoService.getList(userId, topicId, type, page);
    }

    @Override
    public int saveReply(CommentReplyInfo replyInfo) {
        boolean ret  = commentReplyInfoService.save(replyInfo);
        if (ret)
        {
            if (replyInfo.getStatus() == Constant.AUDIT_STATUS_PASS)
            {
                commentTopicInfoService.addReplyCount(replyInfo.getTopicId());
            }

            return 0;
        }
        
        return -1;
    }
}
